package com.example.design.pay.strategy;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePageMergePayRequest;
import com.example.design.pojo.Order;
import com.example.design.utils.Constants;

//策略模式具体策略角色
public class AlipayStrategy implements PayStrategyInterface {
    @Override
    public String pay(Order order) {
        //创建支付宝客户端
        AlipayClient alipayClient = new DefaultAlipayClient(Constants.ALIPAY_GATEWAY_URL,
                Constants.APP_ID,
                Constants.APP_PRIVATE_KEY,
                "JSON",
                "UTF-8",
                Constants.ALIPAY_PUBLIC_KEY,
                Constants.SIGN_TYPE);
        //设置请求参数
        AlipayTradePageMergePayRequest request = new AlipayTradePageMergePayRequest();
        request.setReturnUrl(Constants.CALLBACK_URL);
        request.setBizContent("{\"out_trade_no\":\"" + order.getOrderId() + "\","
                + "\"total_amount\":\"" + order.getPrice() + "\","
                + "\"subject\":\"" + "伟山育琪" + "\","
                + "\"body\":\"" + "商品描述" + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求
        try {
            //执行请求
            String result = alipayClient.pageExecute(request, "GET").getBody();
            System.out.println(result);
            return result;
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
    }
}
